﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
namespace MesGLApp.BLL
{
    /// <summary>
    /// SqlSugar链接
    /// </summary>
    public class SqlSugares
    {

        public static SqlSugarClient GetInstance()
        {
            string connectionString = GetConnectionString();

            // 创建 SqlSugarClient 实例
            var db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = connectionString,
                DbType = SqlSugar.DbType.SqlServer, // 根据你的数据库类型调整
                IsAutoCloseConnection = true, // 自动关闭连接
                InitKeyType = InitKeyType.Attribute // 初始化主键和自增列信息
            }, ctx => {
                //加入在控制台中打印执行SQL的时间，2024-11-06，THOR
                ctx.Aop.OnLogExecuted = (sql, pars) =>
                {
                    Console.WriteLine("Time:" + ctx.Ado.SqlExecutionTime.ToString());
                };

                //加入在控制台中打印执行SQL语句,2024-11-06
                ctx.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));
                };
            });




            return db;
        }

        public static string GetConnectionString()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["RoleDB"].ConnectionString;
            return connectionString;
        }
    }
}
